
import { _decorator, AnimationClip, Component, Node,Animation, SpriteFrame } from 'cc';
import { FSM_PARAM_TYPE_ENUM, PARAMS_NAME_ENUM } from '../../Enum';
import State from '../../Base/State';
import { getInitParamNumber, StateMechine } from '../../Base/StateMechine';
import IdleSubStateMachine from './IdleSubStateMachine';
import TurnLeftSubStateMachine from './TurnLeftSubStateMachine';
const { ccclass, property } = _decorator;

type ParamsValueType = boolean | number;

export interface IParamsValue{
    type:FSM_PARAM_TYPE_ENUM,
    value:ParamsValueType
}

export const getInitParamsTrigger = ()=>{
    return{
            type:FSM_PARAM_TYPE_ENUM.TRIGGER,
            value:false
        }
}

@ccclass('PlayerStateMachine')
export class PlayerStateMachine extends StateMechine {

    async init(){
        this.animationComponent = this.addComponent(Animation)
        this.initParams();
        this.initStateMachines();
        this.initAnimationEvent();

        await Promise.all(this.waitingList);
    }


    initAnimationEvent(){
        this.animationComponent.on(Animation.EventType.FINISHED,()=>{
           const name =  this.animationComponent.defaultClip.name
           const whiteList = ['turn']
           if(whiteList.some(v=>name.includes(v))){
            this.setParams(PARAMS_NAME_ENUM.IDLE,true);
           }

        })
    }

    initStateMachines(){
        this.stateMachines.set(PARAMS_NAME_ENUM.IDLE,new IdleSubStateMachine(this))
        this.stateMachines.set(PARAMS_NAME_ENUM.TURNLEFT,new TurnLeftSubStateMachine(this))
    }

    initParams(){
        this.params.set(PARAMS_NAME_ENUM.IDLE,getInitParamsTrigger())
        this.params.set(PARAMS_NAME_ENUM.TURNLEFT,getInitParamsTrigger());
        this.params.set(PARAMS_NAME_ENUM.DIRECTION,getInitParamNumber());
    }

    run(){
        switch(this.currenState){
            case this.stateMachines.get(PARAMS_NAME_ENUM.TURNLEFT):
            case this.stateMachines.get(PARAMS_NAME_ENUM.IDLE):
                if(this.params.get(PARAMS_NAME_ENUM.TURNLEFT).value){
                    this.currenState = this.stateMachines.get(PARAMS_NAME_ENUM.TURNLEFT);
                }else if(this.params.get(PARAMS_NAME_ENUM.IDLE).value){
                    this.currenState = this.stateMachines.get(PARAMS_NAME_ENUM.IDLE);
                }else{
                     this.currenState = this.currenState;
                }
                break;
            default:
                this.currenState = this.stateMachines.get(PARAMS_NAME_ENUM.IDLE);
        }
    }
}


